/*
 * Copyright 2011 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.bize.knexions.android.knexion.twitter;

import oauth.signpost.OAuth;
import winterwell.jtwitter.OAuthSignpostClient;
import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.Twitter.Status;


/**
 * TODO: Write type comment
 * 
 * @author Joost A. Bloemsma
 */
public class Consumer {
//	public static final String URL_REQUESTTOKEN = "http://twitter.com/oauth/request_token";
//	public static final String URL_ACCESSTOKEN = "http://twitter.com/oauth/access_token";
//	public static final String URL_AUTHORIZE = "http://twitter.com/oauth/authorize";
	
	private static final String CONSUMER_KEY = "0801pztxsBzaIjIdS2DpYQ";
	private static final String CONSUMER_SECRET = "y5c3y1DZlJt2e3GSM8gaUv8rgCJk5XqEbcNw1pVg8XE";
	
	OAuthSignpostClient oauthClient;
	String[] accessToken;
	
//	OAuthProvider provider = new DefaultOAuthProvider(URL_REQUESTTOKEN, URL_ACCESSTOKEN, URL_AUTHORIZE);
//	OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
	/*
	CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(  
	        CONSUMER_KEY, CONSUMER_SECRET, SignatureMethod.HMAC_SHA1);  
	  
	OAuthProvider provider = new DefaultOAuthProvider(consumer,  
			URL_REQUESTTOKEN, URL_ACCESSTOKEN, URL_AUTHORIZE);  
	HttpClient client = new DefaultHttpClient();  
	*/
	
	/**
	 * Constructs a new Consumer instance
	 */
	public Consumer() {
		oauthClient = new OAuthSignpostClient(CONSUMER_KEY, CONSUMER_SECRET, OAuth.OUT_OF_BAND);
	}
	
	/**
	 * @return the oAuth authorize url as String
	 */
	public String getTokenUrl() {
		return oauthClient.authorizeUrl().toString();
		/*
		String url = "NONE";
		try {
			url = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
		} catch (OAuthMessageSignerException e) {
			url = e.getMessage();
		} catch (OAuthNotAuthorizedException e) {
			url = e.getMessage();
		} catch (OAuthExpectationFailedException e) {
			url = e.getMessage();
		} catch (OAuthCommunicationException e) {
			url = e.getMessage();
		}
		return url;
		*/
	}
	
	/**
	 * @param pin
	 */
	public void storeTokens(String pin) {
		oauthClient.setAuthorizationCode(pin);
		accessToken = oauthClient.getAccessToken();
		Twitter twitter = new Twitter("mijn_naam", oauthClient);
		Status status = twitter.getStatus("joostab");
		System.out.println(status);
		/*
		try {
			provider.retrieveAccessToken(consumer, pin);
		} catch (OAuthMessageSignerException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			e.printStackTrace(System.out);
		} catch (OAuthNotAuthorizedException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			e.printStackTrace(System.out);
		} catch (OAuthExpectationFailedException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			e.printStackTrace(System.out);
		} catch (OAuthCommunicationException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			e.printStackTrace(System.out);
		}
		*/
	}
}

